from rest_framework import serializers

from demo_app.models import BookInfo, HeroInfo


# class BookSerializer(serializers.Serializer):
#     """
#     book_data = {
#                 'id': book.id,
#                 'btitle': book.btitle,
#                 'bpub_date': book.bpub_date,
#                 'bread': book.bread,
#                 'bcomment': book.bcomment
#             }
#
#     """
#     # 字段名是 模型中的字段名
#     id = serializers.IntegerField(read_only=True)
#     btitle = serializers.CharField()
#     bpub_date = serializers.DateField()
#     bread = serializers.IntegerField(read_only=True)
#     bcomment = serializers.IntegerField(read_only=True)
#
#     def create(self, validated_data):
#         book = BookInfo(btitle=validated_data.get('btitle'), bpub_date=validated_data.get('bpub_date'))
#         book.save()
#
#         # book = BookInfo.objects.create(btitle=validated_data.btitle, bpub_date=validated_data.bpub_date)
#
#         return book

class BookHeroSerializer(serializers.ModelSerializer):
    class Meta:
        model = HeroInfo
        # fields = '__all__'  # 所有字段
        exclude = ['hbook', 'is_delete']


# # 校验器, 处理结果 1. 不报错, 2. 报错
# def btitle_validator(data):
#     if len(data) > 1:
#         pass
#     else:
#         raise serializers.ValidationError('书名不能是一个字')  # 报错了
#         # return data+data
#
# # 校验器, 处理结果 1. 不报错, 2. 报错
# def btitle_validator2(data):
#     if len(data) < 10:
#         pass
#     else:
#         raise serializers.ValidationError('书名不能大于9个字')  # 报错了
#         # return data+data

# 写入书籍
class BookWriteSerializer(serializers.ModelSerializer):
    # btitle = serializers.CharField(max_length=24, validators=[btitle_validator,btitle_validator2])

    # validators 里面全是校验器, 校验器函数, 处理结果 1. 不报错, 2. 报错

    # 处理器=>  返回处理后的结果,   或者报错
    def validate_btitle(self,data):
    #     # 可以加工书名
    #     # 验证书籍名, 规则是: 不能是一个字
        if len(data)>1:
            pass
        else:
            # raise serializers.ValidationError('书名不能是一个字')  # 报错了
            return data+data

        return data

    # def validate_pw(self, data):
    #     # 不能同时校验两个数据
    #     pass

    # 校验所有的数据, 校验两遍密码是否相同
    def validate(self, datas):
        pw = datas.get('pw')
        pw2 = datas.get('pw2')
        if pw == pw2:
            pass
        else:
            raise serializers.ValidationError('两遍密码不一致')
        return datas

    class Meta:
        model = BookInfo
        fields = '__all__'  # 所有字段
        # fields = ['id','btitle','bpub_date','bread','bcomment']  #
        # exclude = ['is_delete']


class BookSerializer(serializers.ModelSerializer):
    # 英雄信息同时读取
    # 能序列化内容, 是模型所有的属性内容
    # book = BookInfo()
    # book.heros  =>  查询集

    heros = BookHeroSerializer(many=True)

    class Meta:
        model = BookInfo
        fields = '__all__'  # 所有字段
        # fields = ['id','btitle','bpub_date','bread','bcomment']  #
        # exclude = ['is_delete']


#
# class BookWriteSerializer(serializers.Serializer):
#     """
#     book_data = {
#                 'id': book.id,
#                 'btitle': book.btitle,
#                 'bpub_date': book.bpub_date,
#                 'bread': book.bread,
#                 'bcomment': book.bcomment
#             }
#
#     """
#     # 字段名是 模型中的字段名
#     btitle = serializers.CharField()
#     bpub_date = serializers.DateField()
#
#     def create(self, validated_data):
#         book = BookInfo(btitle=validated_data.get('btitle'), bpub_date=validated_data.get('bpub_date'))
#         book.save()
#
#         # book = BookInfo.objects.create(btitle=validated_data.btitle, bpub_date=validated_data.bpub_date)
#
#         return book

class HeroSerializer(serializers.ModelSerializer):
    class Meta:
        model = HeroInfo
        depth = 1  # 深入序列化一层
        fields = '__all__'  # 所有字段


class HeroWriteSerializer(serializers.ModelSerializer):
    class Meta:
        model = HeroInfo
        fields = '__all__'  # 所有字段

# 序列化数据 串  字符串   json
# 结构化数据 二维表,  表, 结构化,  model  结构化

# 结构化  =>  序列化  只需要指定 把结构化数据中的哪些字段, 序列化出去
# 序列化  =>  结构化  1. 校验, 是不是符合结构需求,  2. 如果校验通过, 把数据放在结构中(model)  model.save()